﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Studentium.Entities;

namespace Studentium.UIElements
{
    public partial class PersonalInfoPanel : UserControl
    {
        public PersonalInfoPanel()
        {
            InitializeComponent();

            levelBox.DataSource = Enum.GetValues(typeof (StudentLevel));
        }

        public void LoadStudent(Student student)
        {
            if (!student.IsStudent)
            {
                idBox.Visible = false;
                idLbl.Visible = false;
            }

            idBox.Text = student.BusinessId.ToString();
            lastNameBox.Text = student.LastName;
            firstNameBox.Text = student.FirstName;
            patronymicBox.Text = student.Patronymic;
            //studentChk.Checked = student.IsStudent;
            phoneBox.Text = student.Phone;
            emailBox.Text = student.Email;            
            addressBox.Text = student.Address;
            if (student.Birthday <= DateTime.MinValue || student.Birthday >= DateTime.MaxValue)
                student.Birthday = new DateTime(1900,1,1);
            birthdayPicker.Value = student.Birthday;            
            levelBox.SelectedItem = ((Student)student).Level;
            finishedBox.Checked = student.Finished;
        }

        public bool UpdateStudent(Student student)
        {
            if (student.IsStudent && (lastNameBox.Text.Trim().Length == 0 || firstNameBox.Text.Trim().Length == 0))
            {
                MessageBox.Show("First and last names cann't be empty");
                return false;
            }

            if (!student.IsStudent && firstNameBox.Text.Trim().Length == 0)
            {
                MessageBox.Show("First name cann't be empty");
                return false;
            }

            student.LastName = lastNameBox.Text.Trim();
            student.FirstName = firstNameBox.Text.Trim();
            student.Patronymic = patronymicBox.Text.Trim();
            student.Phone = Regex.Replace(phoneBox.Text, "[^0-9]", "");
            student.Email = emailBox.Text.Trim();
            //student.IsStudent = studentChk.Checked;
            student.Address = addressBox.Text.Trim();
            student.Birthday = birthdayPicker.Value;
            student.Level = (StudentLevel)levelBox.SelectedItem;
            student.Finished = finishedBox.Checked;
            return true;
        }

        public bool WasFinishStatusSet(Student currentStudent)
        {
            return !currentStudent.Finished && finishedBox.Checked;
        }
    }
}
